package com.qjq.glasses_shopping.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qjq.glasses_shopping.mapper.RoleMapper;
import com.qjq.glasses_shopping.pojo.Role;
import com.qjq.glasses_shopping.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    //添加角色
    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    //修改角色
    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    //删除角色
    @Override
    public void delete(Long id) {
        //删除角色
        roleMapper.deleteById(id);
        //删除角色对应的所有权限
        roleMapper.deleteRoleAllPermission(id);
        //删除角色对应的所有用户
        roleMapper.deleteRoleAllAdmin(id);
    }

    //根据id查询角色全部信息
    @Override
    public Role findById(Long id) {
        return roleMapper.findById(id);
    }

    //查询全部角色
    @Override
    public List<Role> findAll() {
        return roleMapper.selectList(null);
    }

    //分页查询角色
    @Override
    public Page<Role> search(int page, int size) {
        return roleMapper.selectPage(new Page<>(page, size), null);
    }

    //修改角色的权限
    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {
        //删除角色的全部权限
        roleMapper.deleteRoleAllPermission(rid);
        //添加角色的全部权限
        for (Long pid:pids){
            roleMapper.addPermissionToRole(rid,pid);
        }
    }
}
